package com.example.tianhao.startproject_413th.bean.iface.service.bean.bean.bean.model;

import com.example.tianhao.startproject_413th.bean.iface.service.bean.bean.bean.bean.WeatherResult;
import com.example.tianhao.startproject_413th.bean.iface.service.bean.bean.bean.iface.WeatherIface;
import com.example.tianhao.startproject_413th.bean.iface.service.bean.bean.bean.iface.WeatherListener;
import com.example.tianhao.startproject_413th.bean.iface.service.bean.bean.bean.service.WeatherService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by TianHao on 2017/3/21.
 */

public class WeatherModel implements WeatherIface {
    private Retrofit retrofit;
    private String BASEURL="http://www.weather.com.cn/";

    public WeatherModel() {
        System.out.println("----1111111111");
        //retrofit实例化
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();//主机地址
    }

    @Override
    public void getWeather(String cityno, final WeatherListener listener) {
        System.out.println("----222222222");
        //Retrofit接口使用3--2
        WeatherService weatherService = retrofit.create(WeatherService.class);
        Call<WeatherResult> call = weatherService.getResult(cityno);
        //Retrofit接口使用3-3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                System.out.println("----333333");
                if(response.isSuccessful() && response.body() != null)
                    listener.onResponse(response.body().getWeatherinfo());
                else
                    listener.onFail("onResponse error");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

}
